/*
 * Copyright (c) ycq_work@163.com
 */

package com.mxg.blog.controller.user;

import com.mxg.blog.common.CqLogger;
import com.mxg.blog.common.DetermineLogin;
import com.mxg.blog.common.ResultCode;
import com.mxg.blog.common.UserOperate;
import com.mxg.blog.entity.Project;
import com.mxg.blog.entity.Skill;
import com.mxg.blog.service.ProjectService;
import com.mxg.blog.service.SkillService;
import com.mxg.blog.service.UserService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: ycq_work@163.com
 * @DATE: 2023/6/25 19:12
 * 负责返回个人信息
 */

@RestController
@RequestMapping("/api")
public class UserController {
    CqLogger cqLogger = CqLogger.getInstance();
    UserOperate userOperate;

    // 装配userService
    private final UserService userService;
    private final SkillService skillService;
    private final ProjectService projectService;

    public UserController (UserService userService, SkillService skillService, ProjectService projectService){
        this.userService = userService;
        this.skillService = skillService;
        this.projectService = projectService;
    }

    // 判断是否登录并创建UserOperate
    private boolean judgeLogin(String userAccount) {
        DetermineLogin determineLogin = DetermineLogin.getInstance(userAccount, userService);
        /*return determineLogin.startJudge();*/
        Integer userId = determineLogin.startJudge();
        if(!userId.equals(DetermineLogin.NOT_LOGIN))
        {
            this.userOperate = UserOperate.getInstance(userId, userService);
            return true;
        }else{
            return false;
        }
    }

    @PostMapping("/avatar")
    public ResponseEntity<Map<String, Object>> getAvatar(@RequestBody Map<String, String> requestMap) {
        Map<String, Object> resultMap = new HashMap<>();

        if(!judgeLogin(requestMap.get("userAccount")))
        {
            resultMap.put(ResultCode.code, ResultCode.WRONG_CODE_NOT_LOGIN);
            resultMap.put(ResultCode.message, ResultCode.WRONG_MESSAGE_NOT_LOGIN);
            cqLogger.print("查找失败");
        }else if(judgeLogin(requestMap.get("userAccount"))){
            resultMap.put(ResultCode.code, ResultCode.SUCCESS_CODE);
            resultMap.put(ResultCode.url, userOperate.getAvatar());
            resultMap.put(ResultCode.message, ResultCode.SUCCESS_MESSAGE);
            cqLogger.print("查找成功");
        }

        //返回数据
        return ResponseEntity.ok(resultMap);
    }

    @PostMapping("/accessInformation")
    public ResponseEntity<Map<String, Object>> accessToPersonalInformation(@RequestBody Map<String, String> requestMap){
        Map<String, Object> resultMap = new HashMap<>();
        if(!judgeLogin(requestMap.get("userAccount")))
        {
            resultMap.put(ResultCode.code, ResultCode.WRONG_CODE_NOT_LOGIN);
            resultMap.put(ResultCode.message, ResultCode.WRONG_MESSAGE_NOT_LOGIN);
            cqLogger.print("查找失败");
        }else if(judgeLogin(requestMap.get("userAccount"))){
            resultMap.put(ResultCode.code, ResultCode.SUCCESS_CODE);
            resultMap.put(ResultCode.nickName, userOperate.getNickName());
            resultMap.put(ResultCode.avatar, userOperate.getAvatar());
            resultMap.put(ResultCode.email, userOperate.getEmail());
            resultMap.put(ResultCode.phone, userOperate.getPhone());
            resultMap.put(ResultCode.message, ResultCode.SUCCESS_MESSAGE);
            cqLogger.print("查找成功");
        }

        return ResponseEntity.ok(resultMap);
    }

    // 修改技能显示状态
    @PostMapping("/changeSkillStatus")
    public ResponseEntity<Map<String, Object>> changeSkillStatus(@RequestBody Map<String, String> requestMap){
        Map<String, Object> resultMap = new HashMap<>();

        if(!judgeLogin(requestMap.get("userAccount")))
        {
            resultMap.put(ResultCode.code, ResultCode.WRONG_CODE_NOT_LOGIN);
            resultMap.put(ResultCode.message, ResultCode.WRONG_MESSAGE_NOT_LOGIN);
            cqLogger.print("查找失败");
        }
        else{
            Integer skillsStatus = Integer.valueOf(requestMap.get("skillsStatus"));
            Integer id = Integer.valueOf(requestMap.get("id"));
            if (skillsStatus == 1)
            {
                skillsStatus = 0;
            }else if(skillsStatus == 0)
            {
                skillsStatus = 1;
            }else
            {
                skillsStatus = 0;
            }
            skillService.changeSkillStatus(skillsStatus, id);
            cqLogger.print("修改状态");
            Skill skill = skillService.getSkillById(id);
            cqLogger.print(skill);
            resultMap.put(ResultCode.code, ResultCode.SUCCESS_CODE);
            resultMap.put(ResultCode.status, skill.getSkillsStatus());
            resultMap.put(ResultCode.message, ResultCode.SUCCESS_MESSAGE);
        }

        return ResponseEntity.ok(resultMap);
    }

    // 修改技能内容
    @PostMapping("/updateSkill")
    public ResponseEntity<Map<String, Object>> updateSkill(@RequestBody Map<String, String> requestMap){
        Map<String, Object> resultMap = new HashMap<>();

        if(!judgeLogin(requestMap.get("userAccount")))
        {
            resultMap.put(ResultCode.code, ResultCode.WRONG_CODE_NOT_LOGIN);
            resultMap.put(ResultCode.message, ResultCode.WRONG_MESSAGE_NOT_LOGIN);
            cqLogger.print("查找失败");
        }
        else{
            String skillContent = requestMap.get("skillContent");
            Integer id = Integer.valueOf(requestMap.get("id"));
            skillService.updateSkill(skillContent, id);
            resultMap.put(ResultCode.code, ResultCode.SUCCESS_CODE);
            resultMap.put(ResultCode.message, ResultCode.SUCCESS_MESSAGE);
        }
        return ResponseEntity.ok(resultMap);
    }

    // 获取所有技能
    @PostMapping("/getSkillsUser")
    public ResponseEntity<Map<String, Object>> getSkills(@RequestBody Map<String, String> requestMap){
        Map<String, Object> resultMap = new HashMap<>();

        if(!judgeLogin(requestMap.get("userAccount")))
        {
            resultMap.put(ResultCode.code, ResultCode.WRONG_CODE_NOT_LOGIN);
            resultMap.put(ResultCode.message, ResultCode.WRONG_MESSAGE_NOT_LOGIN);
            cqLogger.print("查找失败");
        }
        else{
            List<Map<String, Object>> tableData = new ArrayList<>();

            List<Skill> skillList = skillService.getSkills(1);

            for(Skill skill : skillList) {
                Map<String, Object> skillInfo = new HashMap<>();

                skillInfo.put(ResultCode.id, skill.getSkillsId());
                skillInfo.put(ResultCode.content, skill.getSkillsContent());
                skillInfo.put(ResultCode.status, skill.getSkillsStatus());
                tableData.add(skillInfo);
            }
            resultMap.put(ResultCode.code, ResultCode.SUCCESS_CODE);
            resultMap.put(ResultCode.tableData, tableData);
            resultMap.put(ResultCode.message, ResultCode.SUCCESS_MESSAGE);
        }
        return ResponseEntity.ok(resultMap);
    }

    // 添加技能
    @PostMapping("/addSkill")
    public ResponseEntity<Map<String, Object>> addSkill(@RequestBody Map<String, String> requestMap){
        Map<String, Object> resultMap = new HashMap<>();

        if(!judgeLogin(requestMap.get("userAccount")))
        {
            resultMap.put(ResultCode.code, ResultCode.WRONG_CODE_NOT_LOGIN);
            resultMap.put(ResultCode.message, ResultCode.WRONG_MESSAGE_NOT_LOGIN);
            cqLogger.print("查找失败");
        }
        else{
            String skillContent = requestMap.get("skillContent");
            skillService.addSkill(skillContent, 1);
            resultMap.put(ResultCode.code, ResultCode.SUCCESS_CODE);
            resultMap.put(ResultCode.message, ResultCode.SUCCESS_MESSAGE);
        }
        return ResponseEntity.ok(resultMap);
    }

    // 删除技能
    @PostMapping("/deleteSkill")
    public ResponseEntity<Map<String, Object>> deleteSkill(@RequestBody Map<String, String> requestMap){
        Map<String, Object> resultMap = new HashMap<>();

        if(!judgeLogin(requestMap.get("userAccount")))
        {
            resultMap.put(ResultCode.code, ResultCode.WRONG_CODE_NOT_LOGIN);
            resultMap.put(ResultCode.message, ResultCode.WRONG_MESSAGE_NOT_LOGIN);
            cqLogger.print("查找失败");
        }
        else{
            Integer id = Integer.valueOf(requestMap.get("id"));
            cqLogger.print("删除开始");
            skillService.deleteSkill(id);
            cqLogger.print("删除结束");
            resultMap.put(ResultCode.code, ResultCode.SUCCESS_CODE);
            resultMap.put(ResultCode.message, ResultCode.SUCCESS_MESSAGE);
        }
        return ResponseEntity.ok(resultMap);
    }

    // 项目卡片管理
    // 修改项目卡片显示状态
    @PostMapping("/changeProjectStatus")
    public ResponseEntity<Map<String, Object>> changeProjectStatus(@RequestBody Map<String, String> requestMap){
        Map<String, Object> resultMap = new HashMap<>();

        if(!judgeLogin(requestMap.get("userAccount")))
        {
            resultMap.put(ResultCode.code, ResultCode.WRONG_CODE_NOT_LOGIN);
            resultMap.put(ResultCode.message, ResultCode.WRONG_MESSAGE_NOT_LOGIN);
            cqLogger.print("查找失败");
        }
        else{
            Integer projectStatus = new Integer(requestMap.get("projectStatus"));
            Integer id = new Integer(requestMap.get("id"));
            if (projectStatus == 1)
            {
                projectStatus = 0;
            }else if(projectStatus == 0)
            {
                projectStatus = 1;
            }else
            {
                projectStatus = 0;
            }
            projectService.changeProjectStatus(projectStatus, id);
            cqLogger.print("修改状态");
            Project project = projectService.getProjectById(id);
            cqLogger.print(project);
            resultMap.put(ResultCode.code, ResultCode.SUCCESS_CODE);
            resultMap.put(ResultCode.status, project.getProjectStatus());
            resultMap.put(ResultCode.message, ResultCode.SUCCESS_MESSAGE);
        }

        return ResponseEntity.ok(resultMap);
    }

    // 获取所有的项目
    @PostMapping("/getProjectsUser")
    public ResponseEntity<Map<String, Object>> getProjects(@RequestBody Map<String, String> requestMap){
        Map<String, Object> resultMap = new HashMap<>();

        if(!judgeLogin(requestMap.get("userAccount")))
        {
            resultMap.put(ResultCode.code, ResultCode.WRONG_CODE_NOT_LOGIN);
            resultMap.put(ResultCode.message, ResultCode.WRONG_MESSAGE_NOT_LOGIN);
            cqLogger.print("查找失败");
        }
        else{
            List<Map<String, Object>> tableData = new ArrayList<>();

            List<Project> projectsList = projectService.getAllProject(1);

            for(Project project : projectsList) {
                Map<String, Object> projectsInfo = new HashMap<>();

                projectsInfo.put(ResultCode.id, project.getProjectId());
                projectsInfo.put(ResultCode.name, project.getProjectName());
                projectsInfo.put(ResultCode.content, project.getProjectContent());
                projectsInfo.put(ResultCode.harvest, project.getProjectHarvest());
                projectsInfo.put(ResultCode.address, project.getProjectAddress());
                projectsInfo.put(ResultCode.alternateAddress, project.getProjectAddress2());
                projectsInfo.put(ResultCode.status, project.getProjectStatus());
                projectsInfo.put(ResultCode.startDate, project.getProjectBeginTime());
                projectsInfo.put(ResultCode.endDate, project.getProjectEndTime());

                tableData.add(projectsInfo);
            }
            resultMap.put(ResultCode.code, ResultCode.SUCCESS_CODE);
            resultMap.put(ResultCode.tableData, tableData);
            resultMap.put(ResultCode.message, ResultCode.SUCCESS_MESSAGE);
        }
        return ResponseEntity.ok(resultMap);
    }

    // 删除项目
    @PostMapping("/deleteProjects")
    public ResponseEntity<Map<String, Object>> deleteProjects(@RequestBody Map<String, String> requestMap){
        Map<String, Object> resultMap = new HashMap<>();

        if(!judgeLogin(requestMap.get("userAccount")))
        {
            resultMap.put(ResultCode.code, ResultCode.WRONG_CODE_NOT_LOGIN);
            resultMap.put(ResultCode.message, ResultCode.WRONG_MESSAGE_NOT_LOGIN);
            cqLogger.print("查找失败");
        }
        else {
            Integer projectId = new Integer(requestMap.get("id"));


            try {
                projectService.deleteProject(projectId);

                resultMap.put(ResultCode.code, ResultCode.SUCCESS_CODE);
                resultMap.put(ResultCode.message, ResultCode.SUCCESS_MESSAGE);
            }catch (Exception e)
            {
                resultMap.put(ResultCode.code, ResultCode.WRONG_CODE_WRONG_EDIT_ARTICLE);
                resultMap.put(ResultCode.message, ResultCode.WRONG_MESSAGE_WRONG_EDIT_ARTICLE);
            }
        }
        return ResponseEntity.ok(resultMap);
    }
}

